/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package layouteditor.view;

import folder.helper.Folders;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JFrame;
import layoutitems.CurrentLayoutElements;
import layoutitems.ImageElement;
import layoutitems.LayoutElements;
import modelviewcontroller.MouseController;
import xml.handler.LayoutParser;

public class ImageDragAndDrop extends JComponent {

    /********************************************
    *           Fields
    ********************************************/
    
    public int lastMouseX = 0;
    public int lastMouseY = 0;
    public int actMouseX = 0;
    public int actMouseY = 0;
   
    private MouseListener ml;
    private MouseMotionListener mml;
    private static MainMenuBar menuBar;
    private static LayoutParser layoutParser;
    private MouseController mouseController;
    private static JFrame frame;
    private static Folders myFolders;

    /********************************************
    *           Constructor
    ********************************************/
    
    public ImageDragAndDrop() throws IOException {
        
        layoutParser = new LayoutParser(myFolders.element_Listings + "Standard_Element_Listing.xml");
        LayoutElements.standardElements = layoutParser.getLayoutElementsXmlParsed();
        CurrentLayoutElements.allVisibleLayoutElements = new ArrayList<ImageElement>();
        CurrentLayoutElements.allVisibleLayoutElements.addAll(LayoutElements.standardElements);
        mouseController = new MouseController(this);
        initializeMouseListeners();
    }

    private void initializeMouseListeners() {
        mml = new MouseMotionListener() {

            public void mouseDragged(MouseEvent e) {
                actMouseX = e.getX();
                actMouseY = e.getY();
                for (ImageElement image : CurrentLayoutElements.allVisibleLayoutElements) {
                    if (image.getDragable() && (!image.getBlocked())) {
                        image.movePosX(actMouseX - lastMouseX);
                        image.movePosY(actMouseY - lastMouseY);
                        image.getCenter();
                    }
                }
                lastMouseX = actMouseX;
                lastMouseY = actMouseY;
                repaint();

            }

            public void mouseMoved(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        };
        ml = new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {
                    //System.out.println("Rechtsklick")
                    mouseController.onRightMouseButtonClicked(frame.getLayeredPane(),e);
                    //getComponent Methode für ImageElement;
                    //mouseController.onRightMouseButtonClicked(viewElements.get(1).);
                }
                if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
                    //System.out.println("Linksklick");
                    for (ImageElement image:CurrentLayoutElements.allVisibleLayoutElements){image.setMarked(false);}
                    mouseController.onLeftMouseButtonClicked(e);
                    repaint();
                }
                
            }

            public void mousePressed(MouseEvent e) {
                lastMouseX = e.getX();
                lastMouseY = e.getY();
                actMouseX = lastMouseX;
                actMouseY = lastMouseY;
            }

            public void mouseReleased(MouseEvent e) {
                for (ImageElement image : CurrentLayoutElements.allVisibleLayoutElements) {
                    //System.out.println(image.getName()+ "drag" + image.getDragable() + "" + image.getMarked());
                    image.setDragable(false);
                }
            }

            public void mouseEntered(MouseEvent e) {
                // throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseExited(MouseEvent e) {
                // throw new UnsupportedOperationException("Not supported yet.");
            }
        };
        addMouseMotionListener(mml);
        addMouseListener(ml);
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        for (ImageElement image : CurrentLayoutElements.allVisibleLayoutElements) {
            g.drawImage(image.getImage(), image.getPosX(), image.getPosY(), this);
            //if item is marked and enabled for dragging
            if (image.getMarked()) {
                image.getMarkedSquare().drawSquare(g2d, 0.1f);
            }
        }
    }

    public static void main(String args[]) throws IOException {
        String path = ImageDragAndDrop.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String decodedPath = URLDecoder.decode(path, "UTF-8");
        myFolders = new Folders(decodedPath,ImageDragAndDrop.class.getSimpleName());
        //System.out.println(ImageDragAndDrop.class.getSimpleName());
        //System.out.println(decodedPath);
        ImageDragAndDrop imageDragComponent = new ImageDragAndDrop();
        frame = new JFrame("Layout Editor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1280, 480);
        //Hinzufügen der Menüleiste zum Frame
        menuBar = new MainMenuBar(frame);
        frame.setJMenuBar(menuBar.getMenuBar());
        frame.getContentPane().add(imageDragComponent);
        frame.setVisible(true);
    }
}
